$(function () {

    //Date picker
    $('#tradeDate').datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        autoclose: true
    });

    $("#btnSend").click(function(){
        //makeClean();
        saveBankRequest('1')
    });
    $("#btnSave").click(function () {
        saveBankRequest('0')
    });
    $("#btnExit").click(function () {

    });
    $('.panelclose').click(function () {
        $('#addSuccessMsg').hide();
        $('#addFailMsg').hide();
    });
    $('.sign-out').click(function () {
        $('#addSuccessMsg').hide();
        $('#addFailMsg').hide();
    });

    if(bankRequestType=='0'){
        $('#bankRequestTypeDiv').html("银行订单号");
    }else{
        $('#bankRequestTypeDiv').html("银行卡号");
    }

});

function makeClean()
{
    $('input').each(function (index ,item) {
        $(this).val('');
    })
    $('select').each(function (index, item) {
        $(this).val('');
    });
    $('textarea').each(function () {
        $(this).val('');
    })
    $('#fileListDiv').empty();
}

function saveBankRequest(state)
{
    var par = new Object();

    par.bankRequestNo = $('#bankRequestNo').val();
    par.requestType =$('#requestType').val();
    par.emergencyState =$('#emergencyState').val();
    par.interceptState = $('#interceptState').val();
    par.bankRequestType =bankRequestType;
    par.tradeDate = $('#tradeDate').val();
    par.tradeMoney =$('#tradeMoney').val();
    par.realName = $('#realName').val();
    par.certno = $('#certno').val();
    par.telephone = $('#telephone').val();
    par.bankRequestState = state;
    par.remark = $('#remark').val();
    var fileName = "" ,count =0 ;
    $("input[name='attachFile']").each(function(index, item){
        fileName =$(item).val()+","+fileName;
        count++;
    });
    par.bankAttachFile = fileName;
    par.bankAttachCount = count;
    $.ajax({
        type: "POST",
        url: queryPath+"/addBankOrderRequest.do",
        data: par,
        dataType: "json",
        async: false,
        success: function(msg) {
            if(msg.success){
                $('#addSuccessMsg').show();
            }else{
                $('#addFailMsg').show();
                $('#addErrMsg').append(msg.message);
            }
        }
    });

}

function subForm(){
    $("#myForm").ajaxSubmit({
        type:"post",  //提交方式
        url:queryPath+"/uploadFile.do",
        dataType:"json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success:function(data){ //提交成功的回调函数
            if(data.success){
                var tmp =  "<span data='"+data.fileName+"'>附件：<input type=\"hidden\" name=\"attachFile\" value='"
                    +data.fileName +"'> <a href=\"javascript:void(0)\" onclick=\"downloadFile('"+data.fileName+"')\" >"+data.fileName
                    +"</a> <i class=\"fa fa-fw fa-trash-o delete\" onclick=\"deleteFile('"+data.fileName+"')\"></i></span>";
                $('#fileListDiv').append(tmp);
            }
        }
    });
}

function deleteFile(fileName)
{
    $.ajax({
        type: "POST",
        url: queryPath+"/deleteFile.do?fileName="+fileName,
        dataType: "json",
        async: false,
        success: function(msg) {
            if(msg.success)
            {
                $("span[data='"+fileName+"']").remove();
            }
        }
    });

}

function downloadFile(fileName)
{
    window.location.href = queryPath+"/downloadFile.do?fileName"+fileName;
}